/**
 * Work with SQLite
 * Persistence in computer science refers to the characteristic of state that outlives the process that created it. 
 * Without this capability, state would only exist in RAM, and would be lost when this RAM loses power, such as a computer shutdown.[citation needed]
 * This is achieved in practice by storing the state as data in non-volatile storage such as a hard drive or flash memory.
 * Picture editing programs or word processors, for example, achieve state persistence by saving their documents to files.
 
 * @author Apocrypha
 */
package ganga.custodian;

import ganga.sensors.AccelerometerData;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.content.ContentValues;
import android.content.Context;
import android.database.DatabaseUtils.InsertHelper;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	public final int accelerationXColumnInd;
	public final int accelerationYColumnInd;
	public final int accelerationZColumnInd;
	private static final String DATABASE_NAME = "/sdcard/Custodian.sqlite";
	// private static final String DATABASE_NAME = "Custodian.sqlite";

	private static final int DATABASE_VERSION = 13;
	@SuppressWarnings("unused")
	private final Context context;
 	/**
	 * Complete format is: String dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
	 */
	private String dateTimeFormat = "HH:mm:ss.SSS";
	SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormat, Locale.US);

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
		InsertHelper helper = new InsertHelper(getReadableDatabase(), "Acceleration");
		accelerationXColumnInd = helper.getColumnIndex("X");
		accelerationYColumnInd = helper.getColumnIndex("Y");
		accelerationZColumnInd = helper.getColumnIndex("Z");
		helper.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sessionSql = "CREATE TABLE Session (SessionId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, StartDateTime DATETIME,Position TEXT,Activity TEXT,Movement TEXT,HumanId INTEGER,DeviceId INTEGER,Duration BIGINT);";
		String accelerationSql = "CREATE TABLE Acceleration(SessionId INTEGER, DateTimeStamp TEXT, EventTimeStamp TEXT, X FLOAT, Y FLOAT, Z FLOAT)";
		String locationSql = "CREATE TABLE Location(SessionId INTEGER, DateTimeStamp TEXT, Long FLOAT, Lat FLOAT)";
		db.beginTransaction();
		try {
			db.execSQL(sessionSql);
			db.execSQL(accelerationSql);
			db.execSQL(locationSql);
			db.setTransactionSuccessful();

			Log.d(DATABASE_NAME, "Ok");
		} catch (SQLException ex) {
			Log.e("Unable to create Custodian database", ex.toString());
			throw ex;

		} finally {
			db.endTransaction();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	/**
	 * ﻿ * Создание новой измерительной сессии версия со строками ﻿ * @param
	 * startDateTime ﻿ * @param position ﻿ * @param activity ﻿ * @param movement
	 * ﻿ * @return ﻿
	 */
	public long insertMeasurementSession(Timestamp startDateTime,
			String position, String activity, String movement) {
		long result = 0;
		try {
			ContentValues values = new ContentValues();
			values.put("StartDateTime", startDateTime.toGMTString());
			values.put("Position", position);
			values.put("Activity", activity);
			values.put("Movement", movement);
			values.put("HumanId", 2);// 1-GIB, 2-Juko //3-Cuarto //7-Apocrypha
			values.put("DeviceId", 2);// 1-HTC //2-Samsung //
			result = getWritableDatabase().insert("Session", null, values);

			if (result == -1)
				Log.e(DATABASE_NAME, "Insert fail");
		} catch (Exception ex) {
			ex.printStackTrace();

		}
		return result;
	}

	public void updateMeasurementSession(long sessionId, long duration) {
		try {
			ContentValues values = new ContentValues();
			values.put("Duration", duration);
			String[] whereArgs = new String[] { Long.toString(sessionId) };
			getWritableDatabase().update("Session", values, "SessionId=?",
					whereArgs);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void insertAcc(long sessionId, AccelerometerData data) {
		try {
			ContentValues values = new ContentValues();
			values.put("SessionId", sessionId);
			values.put("DateTimeStamp", data.systemTimestamp);
			values.put("EventTimeStamp ", data.eventTimestamp);
			// TODO:transfer to processing class
			float x = (float) Math.round(data.ax * 100) / 100;
			values.put("X", x);
			values.put("Y", data.ay);
			values.put("Z", data.az);
			long result = getWritableDatabase().insert("Acceleration", null,
					values);

			if (result == -1)
				Log.e(DATABASE_NAME, "Insert Acc data fail");

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}